package com.shujia.five;

import org.junit.Test;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

public class TreeSetDemo2 {


    /*
    int i = -name.compareTo(o.name);

        if (i == 0) {// name值相同
            // 比较age
            return age.compareTo(o.getAge());
        } else {
            return i;
        }
     */

    public static void main(String[] args) {
        // 排序规则只在当前方法适用
        Set<Doctor> set = new TreeSet<>(new Comparator<Doctor>() {
            @Override
            public int compare(Doctor o1, Doctor o2) {
                int i = -o1.getName().compareTo(o2.getName());

                if (i == 0) {// name值相同
                    // 比较age
                    return o1.getAge().compareTo(o2.getAge());
                } else {
                    return i;
                }
            }
        });
        Doctor doc = new Doctor("zs", 20);
        Doctor doc1 = new Doctor("ls", 24);
        Doctor doc2 = new Doctor("ww", 22);
        Doctor doc3 = new Doctor("zs", 11);
        Doctor doc4 = new Doctor("ls", 30);

        set.add(doc);
        set.add(doc1);
        set.add(doc2);
        set.add(doc3);
        set.add(doc4);

        System.out.println(set);
    }
}
